//
//  UserLogin.m
//  cocoapodText
//
//  Created by apple on 16/9/19.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "NetTool.h"
#import "UserLogin.h"
#import "AFNetworking.h"

@interface UserLogin()

@end
@implementation UserLogin

- (void)fsgy_api_user_login_withName:(NSString *)username Pass:(NSString *)password{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    //申明返回的结果是json类型
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSString *neturl=[NetTool fsgy_login];
    NSDictionary *params=@{@"action":@"init",@"username":username,@"password":password,@"sslogon":@"true",@"logoff":@"0"};
    [session GET:neturl parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        //缓存session
        NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
        NSDictionary *allHeaders = response.allHeaderFields;
        NSString *srcStr = [allHeaders objectForKey:@"Set-Cookie"];
        NSArray *strarray = [srcStr componentsSeparatedByString:@";"];
        NSString *JSESSIONID_ALL=strarray[0];
        NSArray *strarray_j = [JSESSIONID_ALL componentsSeparatedByString:@"="];
        NSString *JSESSIONID=strarray_j[1];
        [CommonTool updateValueWithKey:@"JSESSIONID" value:JSESSIONID];
        
        if([_delegate respondsToSelector:@selector(onLogin: result:)]){
            [_delegate onLogin:self result:responseObject];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error=%@",error);
    }];
}

- (void)fsgy_api_user_logout_withName:(NSString *)username Pass:(NSString *)password{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    //申明返回的结果是json类型
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSString *neturl=[NetTool fsgy_login];
    NSDictionary *params=@{@"action":@"init",@"username":username,@"password":password,@"sslogon":@"true",@"logoff":@"1"};
    [session GET:neturl parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if([_delegate respondsToSelector:@selector(onLogout:result:)]){
            [_delegate onLogout:self result:responseObject];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error=%@",error);
    }];
}
@end
